<?php
include("./config.php");
include("./utils.php");


$op = $_GET['op'];
$id=$_GET['id'];
$appid=$_GET['appid'];
$lang = getFromMap($_GET, 'lang', 'cn');
if (lang == 'en') {
    $op = $op.'-en';
}

function get_sql($appid, $op, $id){
  $sql = "";
  switch($op){
  # -------------------------------------------------------
  case 'get-sns-by-shortcut':
  case 'get-sns-by-shortcut-en':
    # 快捷键下的评论
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql="SELECT `id`, `userid`, `appid`, `shortcutid`, `parent`, `content`, `status`, `up`, `update_time` FROM `sns` WHERE `appid`='$appid' AND `shortcutid`='$shortcutid' LIMIT 20";
    break;

  case 'get-sns-by-sns':
  case 'get-sns-by-sns-en':
    # 查看对话
    $parent = safe(getGET('parent'));
    $sql="SELECT `id`, `userid`, `appid`, `shortcutid`, `parent`, `content`, `status`, `up`, `update_time` FROM `sns` WHERE `parent`='$parent' LIMIT 20";
    break;

  case 'get-sns-by-status':
  case 'get-sns-by-status-en':
    # 查询特定状态的评论
    $status = safe(getGET('status'));
    $sql="SELECT `id`, `userid`, `appid`, `shortcutid`, `parent`, `content`, `status`, `up`, `update_time` FROM `sns` WHERE `status`&'$status' LIMIT 20";
    break;

  case 'get-sns-by-app':
  case 'get-sns-by-app-en':
    # 查询app下的评论
    $appid = safe(getGET('appid'));
    $sql="SELECT `id`, `userid`, `appid`, `shortcutid`, `parent`, `content`, `status`, `up`, `update_time` FROM `sns` WHERE `appid`='$appid' LIMIT 20";
    break;

  # -------------------------------------------------------
  case 'user':
    $sql = "SELECT `name` FROM `user` WHERE `appid`=$appid";
    break;
  case 'info':
    $sql = "SELECT `id`, `value` FROM `info` WHERE `appid`=$appid";
    break;
  case 'str':
    $sql = "SELECT `id`, `value` FROM `str` WHERE `appid`=$appid";
    break;

#------------------------
  case 'comment-count':
  case 'comment-count-en':
    $appid = safe(getGET('appid'));
    $sql = "SELECT `shortcutid`, `appid`, COUNT(shortcutid) as `count` FROM `sns` WHERE `appid`='$appid' GROUP BY `shortcutid`";
    break;
  case 'one-shortcut':
  case 'one-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND `shortcutid`=$shortcutid";
    break;
  case 'one-show-shortcut':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND `shortcutid`=$shortcutid";
    break;
  case 'one-show-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name_en` as name, `des_en` as des, `context_en` as context, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND `shortcutid`=$shortcutid";
    break;
  case 'next-show-shortcut':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`>$shortcutid) ORDER BY shortcutid ASC LIMIT 1";
    break;
  case 'next-show-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name_en` as name, `des_en` as des, `context_en` as context, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`>$shortcutid) ORDER BY shortcutid ASC LIMIT 1";
    break;

  case 'prev-show-shortcut':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`<$shortcutid) ORDER BY shortcutid DESC LIMIT 1";
    break;
  case 'prev-show-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name_en` as name, `des_en` as des, `context_en` as context, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`<$shortcutid) ORDER BY shortcutid DESC LIMIT 1";
    break;


  case 'one-app':
  case 'one-app-en':
    $appid = safe(getGET('appid'));
    $sql = "SELECT `appid`, `name`, `des`, `name_en`, `des_en`, `skin`, `shortcut_order`,`logo` FROM `app` WHERE `appid`=$appid";
    break;

  case 'all-app':
    $sql = "SELECT `appid`, `name`, `des`, `skin`,`logo` FROM `app` ORDER BY appid ASC LIMIT 500";
    break;
  case 'all-app-en':
    $sql = "SELECT `appid`, `name_en` as name, `des_en` as des, `skin` FROM `app`,`logo` ORDER BY appid ASC LIMIT 500";
    break;
  case 'all-shortcut':
    $appid = safe(getGET('appid'));
    $sql = "SELECT `shortcutid`, `shortcut`, `name`, `des`, `context`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid ORDER BY shortcutid LIMIT 500";
    break;
  case 'all-shortcut-en':
    $appid = safe(getGET('appid'));
    $sql = "SELECT `shortcutid`, `shortcut`, `name_en` as name, `des_en` as des, `context_en` as context, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid ORDER BY shortcutid LIMIT 500";
    break;

  case 'inc-up':
  case 'inc-up-en':
    $sql = "UPDATE `shortcut` SET `up`=`up`+1 WHERE `appid`=$appid AND `shortcutid`=$id AND ((CURRENT_TIMESTAMP-update_time)>60*3) LIMIT 1";
    mysql_query($sql);
    $sql = "SELECT `down`, `up` from `shortcut` WHERE `appid`=$appid AND `shortcutid`=$id";
    break;
  case 'inc-down':
  case 'inc-down-en':
    $sql = "UPDATE `shortcut` SET `down`=`down`+1 WHERE `appid`=$appid AND `shortcutid`=$id AND ((CURRENT_TIMESTAMP-update_time)>60*3) LIMIT 1";
    mysql_query($sql);
    $sql = "SELECT `down`, `up` from `shortcut` WHERE `appid`=$appid AND `shortcutid`=$id";
    break;

  case 'next-shortcut':
  case 'next-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`>$shortcutid) ORDER BY shortcutid ASC LIMIT 1";
    break;

  case 'prev-shortcut':
  case 'prev-shortcut-en':
    $appid = safe(getGET('appid'));
    $shortcutid = safe(getGET('id'));
    $sql = "SELECT `shortcutid`, `appid`, `shortcut`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`, `up`,`down` FROM `shortcut` WHERE `appid`=$appid AND (`shortcutid`<$shortcutid) ORDER BY shortcutid DESC LIMIT 1";
    break;


  default:
    break;
  }
  return $sql;
}


if ($op == "all" || $op == "all-en") {
  $result = array();
  $ops = array("one-app", "comment-count", "all-shortcut");
  if ($lang == 'en'){
      $ops = array("one-app", "comment-count", "all-shortcut-en");
  }
  foreach ($ops as $x) {
    $sql = get_sql($appid, $x, $id);
    $tmp = get_result($sql);
    $result[$x] = $tmp;
  }
} else {
  $sql = get_sql($appid, $op, $id);
  if($sql != ""){
    $result = get_result($sql);
  }
}

if (empty($result)) {
  echo "{code:0x1002, msg:'no more data'}";
} else {
  echo json_encode($result);
}

?>

